package com.ablife.limiter.cxf;

import com.ablife.limiter.algorithm.CountRateLimiter;
import com.ablife.limiter.service.ReturnService;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component("limiterService")
@WebService
public class LimiterServiceImpl implements LimiterService {
    @Autowired
    CountRateLimiter rateLimiter;
    @Autowired
    ReturnService returnService;

    public LimiterServiceImpl() {
    }

    public String limit(String channel, String tradeCode, String type) {
        if (!StringUtils.isEmpty(channel) && !StringUtils.isEmpty(tradeCode) && !StringUtils.isEmpty(type)) {
            boolean flag = this.rateLimiter.doLimit(channel, tradeCode, type);
            return this.returnService.getReturnString(channel, tradeCode, flag);
        } else {
            return "{\"code\":\"9\",\"description\":\"参数有误\"}";
        }
    }
}
